/*
 * @lc app=leetcode.cn id=150 lang=javascript
 *
 * [150] 逆波兰表达式求值
 */

// @lc code=start
/**
 * @param {string[]} tokens
 * @return {number}
 */
 var evalRPN = function (tokens) {
  const myStack = []
  let sum = 0
  const rpn = (a, b, evalStr) => {
    switch (evalStr) {
      case '+':
        return b + a
      case '-':
        return b - a
      case '*':
        return b * a
      case '/':
        return (b / a) | 0
    }
  }
  for (item of tokens) {
    if (isNaN(item)) {
      sum = rpn(Number(myStack.pop()), Number(myStack.pop()), item)
      myStack.push(sum)
    } else {
      myStack.push(item)
    }
  }
  return myStack[0]
};
// @lc code=end

